﻿#include "framework.h"
#include "Const.h"
#include "Resource.h"
#include "RivalWindow.h"

// 函数声明
static void RivalWindow_Delete(RivalWindow*);
static void RivalWindow_OnCancel(RivalWindow*);
static void RivalWindow_OnOK(RivalWindow*);
static INT_PTR CALLBACK RivalWindow_Proc(HWND, UINT, WPARAM, LPARAM);
static void RivalWindow_SetUsernameList(RivalWindow*, TCHAR(*)[USERNAME_BUF_LEN]);

/**
 * 构造方法。
 * @param parent    父窗口
 */
RivalWindow* New_RivalWindow(HWND parent) {
    RivalWindow* rivalWindow = (RivalWindow*)calloc(1, sizeof(RivalWindow));
    
    // 初始化数据
    rivalWindow->mHandle = CreateDialog(NULL, MAKEINTRESOURCE(IDD_RIVAL_WINDOW), parent, RivalWindow_Proc);
    SetWindowLongPtr(rivalWindow->mHandle, GWLP_USERDATA, (LONG_PTR)rivalWindow);
    rivalWindow->mListViewUsers = GetDlgItem(rivalWindow->mHandle, IDC_USER_LIST);

    // 初始化函数
    rivalWindow->deleteSelf = RivalWindow_Delete;
    rivalWindow->setUsernameList = RivalWindow_SetUsernameList;

    return rivalWindow;
}

/**
 * 删除对手列表窗口。
 * @param rivalWindow   对手列表窗口
 */
static void RivalWindow_Delete(RivalWindow* rivalWindow) {
    DestroyWindow(rivalWindow->mHandle);
    free(rivalWindow);
}

/**
 * "取消"按钮的单击响应方法。
 * @param rivalWindow   对手列表窗口
 */
static void RivalWindow_OnCancel(RivalWindow* rivalWindow) {
    // 关闭窗口
    ShowWindow(rivalWindow->mHandle, SW_HIDE);
}

/**
 * "确定"按钮的单击响应方法。
 * @param rivalWindow   对手列表窗口
 */
static void RivalWindow_OnOK(RivalWindow* rivalWindow) {
    char selectedName[USERNAME_BUF_LEN] = { 0 };
    HWND parent = GetParent(rivalWindow->mHandle);

    // 获取用户选择的名字
    int sel = (int)SendMessage(rivalWindow->mListViewUsers, LB_GETCURSEL, 0, 0);
    if (sel == -1) {
        return;
    }
    SendMessage(rivalWindow->mListViewUsers, LB_GETTEXT, sel, (LPARAM)selectedName);
    SendMessage(parent, UM_GET_RIVAL_NAME, (WPARAM)selectedName, 0);

    // 关闭窗口
    ShowWindow(rivalWindow->mHandle, SW_HIDE);
}

/**
 * 窗口过程函数。
 * @param window     窗口
 * @param message    事件标识
 * @param wParam     事件相关信息
 * @param lParam     事件相关信息
 * @return 事件在此被处理则返回 TRUE，否则返回 FALSE
 */
static INT_PTR CALLBACK RivalWindow_Proc(HWND window, UINT message, WPARAM wParam, LPARAM lParam) {
    RivalWindow* rivalWindow = (RivalWindow*)GetWindowLongPtr(window, GWLP_USERDATA);
    WORD id = LOWORD(wParam);
    UNREFERENCED_PARAMETER(lParam);

    switch (message) {
        case WM_COMMAND:
            switch (id) {
                case IDCANCEL:
                    RivalWindow_OnCancel(rivalWindow);
                    return TRUE;
                case IDOK:
                    RivalWindow_OnOK(rivalWindow);
                    return TRUE;
            }
            break;
    }
    return FALSE;
}

/**
 * 设置用户名列表。
 * @param rivalWindow   对手列表窗口
 * @param userListNode  用户名列表
 */
static void RivalWindow_SetUsernameList(RivalWindow* rivalWindow, TCHAR(*usernameList)[USERNAME_BUF_LEN]) {
    int i;

    // 删除所有 item
    int count = (int)SendMessage(rivalWindow->mListViewUsers, LB_GETCOUNT, 0, 0);
    for (i = count - 1; i >= 0; i--) {
        SendMessage(rivalWindow->mListViewUsers, LB_DELETESTRING, i, 0);
    }

    // 添加 item
    for (i = 0; i < MAX_USER_COUNT; i++) {
        if (usernameList[i][0] != '\0') {
            SendMessage(rivalWindow->mListViewUsers, LB_ADDSTRING, 0, (LPARAM)usernameList[i]);
        }
    }
}
